package com.bw.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.bw.dao.AdminRoleMapper;
import com.bw.dao.RoleMapper;
import com.bw.pojo.order.Order;
import com.bw.pojo.order.OrderItem;
import com.bw.pojo.order.Orders;
import com.bw.pojo.system.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.bw.dao.AdminMapper;
import com.bw.entity.PageResult;
import com.bw.service.system.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private RoleMapper roleMapper;


    /**
     * 返回全部记录
     * @return
     */
    public List<Admin> findAll() {
        return adminMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Admin> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Admin> admins = (Page<Admin>) adminMapper.selectAll();
        return new PageResult<Admin>(admins.getTotal(),admins.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Admin> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return adminMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Admin> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Admin> admins = (Page<Admin>) adminMapper.selectByExample(example);
        return new PageResult<Admin>(admins.getTotal(),admins.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Admin findById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public AdminPassword findByName(String name) {
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginName",name);
        List<Admin> admins = adminMapper.selectByExample(example);
        Admin admin1 = new Admin();
        AdminPassword adminPassword = new AdminPassword();
        for (Admin admin : admins) {
            admin1.setId(admin.getId());
            admin1.setPassword(admin.getPassword());
            admin1.setLoginName(admin.getLoginName());
            admin1.setStatus(admin.getStatus());
            adminPassword.setAdmin(admin1);
            adminPassword.setPassword("");
        }
        return adminPassword;
    }


    /**
     * 新增
     * @param admin
     */
    public void add(Admin admin) {
        adminMapper.insert(admin);
    }

    /**
     * 修改
     * @param admin
     */
    public void update(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public void updatePassword(AdminPassword adminPassword) {
        Admin admin = adminPassword.getAdmin();
        Admin admin1 = adminMapper.selectByPrimaryKey(admin.getId());
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        //String encode = bCryptPasswordEncoder.encode(admin.getPassword());
        if(bCryptPasswordEncoder.matches(admin.getPassword(),admin1.getPassword())){
            String encode1 = bCryptPasswordEncoder.encode(adminPassword.getPassword());
            admin1.setPassword(encode1);
            adminMapper.updateByPrimaryKeySelective(admin1);
        }
    }

    public void updateAdmins(Admins admins) {
        Admin admin = admins.getAdmin();

        //先删除
        Example example = new Example(AdminRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("adminId",admin.getId());
        List<AdminRole> adminRoles = adminRoleMapper.selectByExample(example);
        for (AdminRole adminRole : adminRoles) {
            adminRoleMapper.delete(adminRole);
        }


        //在增加
        AdminRole adminRole = new AdminRole();
        List<Role> roleList = admins.getRoleList();
        for (Role role : roleList) {
            adminRole.setAdminId(admin.getId());
            adminRole.setRoleId(role.getId());
            adminRoleMapper.insert(adminRole);
        }
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Integer id) {
        adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Admins findAdminsById(Integer id) {
        Admins admins = new Admins();
        //查询admin
        Admin admin = adminMapper.selectByPrimaryKey(id);
        //赋值admin
        admins.setAdmin(admin);
        //查询中间表数据
        Example example = new Example(AdminRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("adminId",id);
        List<AdminRole> adminRoles = adminRoleMapper.selectByExample(example);
        List<Role> roleList = new ArrayList<Role>();
        for (AdminRole adminRole : adminRoles) {
            //获取role对象
            Role role = roleMapper.selectByPrimaryKey(adminRole.getRoleId());
            //添加到roleList列表
            roleList.add(role);
        }
        admins.setRoleList(roleList);
        return admins;
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 用户名
            if(searchMap.get("loginName")!=null && !"".equals(searchMap.get("loginName"))){
                criteria.andEqualTo("loginName",searchMap.get("loginName"));
            }
            // 密码
            if(searchMap.get("password")!=null && !"".equals(searchMap.get("password"))){
                criteria.andEqualTo("password",searchMap.get("password"));
            }
            // 状态
            if(searchMap.get("status")!=null && !"".equals(searchMap.get("status"))){
                criteria.andEqualTo("status",searchMap.get("status"));
            }

            // id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }

        }
        return example;
    }

}
